package com.matt.foundation.sqlOperator;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.matt.foundation.constant.EvaluatingConstant;
import com.matt.foundation.utils.ModelUtil;

import java.io.InputStream;
import java.sql.*;
import java.util.List;
import java.util.Map;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/8/16
 * *******************************
 */
public class EvaluatingOperator {

    public static final String COURSE_BEFORE_FILE = "course_before.txt";

    public static final String MONTH_FILE = "month.txt";

    public static final String COURSE_END_FILE="course_end.txt";

    public static final String remoteUrl = "jdbc:mysql://localhost:3306/vega";

    public static final String remoteName = "root";

    public static final String remotePWd = "123456";


    public static void main(String[] args) throws Exception {
//        evaluationECImport();
//        evaluatingImport(MONTH_FILE, EvaluatingConstant.EvaluatingType.MONTH.codeOf());
//        evaluatingImport(COURSE_BEFORE_FILE, EvaluatingConstant.EvaluatingType.COURSE_BEFORE.codeOf());
    }



    public static void evaluatingImport(String fileName, Integer code) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection remoteConn = DriverManager.getConnection(remoteUrl, remoteName, remotePWd);

        InputStream inputStream = EvaluatingOperator.class.getClassLoader().getResourceAsStream(fileName);
        List list = new ObjectMapper().readValue(inputStream, List.class);
        sqlProcess(remoteConn, list, code);
    }



    public static void sqlProcess(Connection remoteConn, List list, Integer code) throws Exception {

        list.forEach(o -> {
            Integer month = Integer.valueOf(((Map) o).get("month").toString());
            String evaluatingId = insertEvaluating(remoteConn, code, month);

            List games = (List) ((Map) o).get("games");
            System.out.println(month);

            games.forEach(g->{
                System.out.println(g);
                String gameId = insertGames(remoteConn, g.toString());
                insertRelEvaluatingGame(remoteConn, evaluatingId, gameId);
            });


            List subs = (List) ((Map) o).get("subs");
            subs.forEach(s -> {
                String name = ((Map) s).get("name").toString();
                System.out.println(name);
                List questions = (List) ((Map) s).get("questions");
                questions.forEach(q->{
                    System.out.println(q);
                    insertOptions(remoteConn, name, EvaluatingConstant.OptionType.ATTENTION.getValueOf(name).codeOf(), q.toString(), evaluatingId);
                });

            });
        });
    }



    private static String insertEvaluating(Connection remoteConn, Integer type, Integer month) {

        try {
            PreparedStatement remotePreparedStatement = null;


            String id = ModelUtil.getCleanUUID();
            String insertEvaluating = "insert evaluating (id, type , month, created, deleted) VALUES (?, ?, ?, ?, ?)";
            remotePreparedStatement = remoteConn.prepareStatement(insertEvaluating);
            remotePreparedStatement.setString(1, id);
            remotePreparedStatement.setInt(2, type);
            remotePreparedStatement.setInt(3, month);
            remotePreparedStatement.setDate(4, new Date(System.currentTimeMillis()));
            remotePreparedStatement.setBoolean(5, false);
            remotePreparedStatement.execute();
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String insertGames(Connection remoteConn, String name){

        try {
            PreparedStatement remotePreparedStatement = null;

            String id = ModelUtil.getCleanUUID();
            String insertEvaluating = "insert evaluating_game (id, name , resourceUrl, created, deleted) VALUES (?, ?, ?, ?, ?)";
            remotePreparedStatement = remoteConn.prepareStatement(insertEvaluating);
            remotePreparedStatement.setString(1, id);
            remotePreparedStatement.setString(2, name);
            remotePreparedStatement.setString(3, null);
            remotePreparedStatement.setDate(4, new Date(System.currentTimeMillis()));
            remotePreparedStatement.setBoolean(5, false);
            remotePreparedStatement.execute();
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String insertRelEvaluatingGame(Connection remoteConn, String evaluatingId, String gameId){

        try {
            PreparedStatement remotePreparedStatement = null;

            String id = ModelUtil.getCleanUUID();
            String insertEvaluating = "insert rel_evaluating_game (id, evaluatingId, gameId) VALUES (?, ?, ?)";
            remotePreparedStatement = remoteConn.prepareStatement(insertEvaluating);
            remotePreparedStatement.setString(1, id);
            remotePreparedStatement.setString(2, evaluatingId);
            remotePreparedStatement.setString(3, gameId);
            remotePreparedStatement.execute();
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String insertOptions(Connection remoteConn, String name, Integer type, String title, String evaluatingId){

        try {

            PreparedStatement remotePreparedStatement = null;

            String id = ModelUtil.getCleanUUID();
            String insertEvaluating = "insert evaluating_question (id, typeName, typeCode, title, score, evaluatingId, created, deleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            remotePreparedStatement = remoteConn.prepareStatement(insertEvaluating);
            remotePreparedStatement.setString(1, id);
            remotePreparedStatement.setString(2, name);
            remotePreparedStatement.setInt(3, type);
            remotePreparedStatement.setString(4, title);
            remotePreparedStatement.setInt(5, 1);
            remotePreparedStatement.setString(6, evaluatingId);
            remotePreparedStatement.setDate(7, new Date(System.currentTimeMillis()));
            remotePreparedStatement.setBoolean(8, false);
            remotePreparedStatement.execute();
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void evaluationECImport() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection remoteConn = DriverManager.getConnection(remoteUrl, remoteName, remotePWd);

        InputStream inputStream = EvaluatingOperator.class.getClassLoader().getResourceAsStream(COURSE_END_FILE);
        List list = new ObjectMapper().readValue(inputStream, List.class);
        list.forEach(o -> {
            String type = ((Map) o).get("type").toString();
            String title = ((Map) o).get("title").toString();
            String evaluatingId = insertEvaluatingEC(remoteConn, EvaluatingConstant.EvaluatingCEType.BEGIN_ACTIVITY.getValueOf(type).codeOf(), type, title);

            List options = (List) ((Map) o).get("options");
            Integer score = 5;
            for (int i =0; i< options.size(); i++){
                insertEvaluatingECOption(remoteConn,  options.get(i).toString(), evaluatingId, score--);
            }
        });
    }
    private static String insertEvaluatingEC(Connection remoteConn, Integer typeCode, String typeName, String title) {

        try {
            PreparedStatement remotePreparedStatement = null;

            String id = ModelUtil.getCleanUUID();
            String insertEvaluating = "insert evaluating_ce (id, typeCode, typeName,  title, created, deleted) VALUES (?, ?, ?, ?, ?, ?)";
            remotePreparedStatement = remoteConn.prepareStatement(insertEvaluating);
            remotePreparedStatement.setString(1, id);
            remotePreparedStatement.setInt(2, typeCode);
            remotePreparedStatement.setString(3, typeName);
            remotePreparedStatement.setString(4, title);
            remotePreparedStatement.setDate(5, new Date(System.currentTimeMillis()));
            remotePreparedStatement.setBoolean(6, false);
            remotePreparedStatement.execute();
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String insertEvaluatingECOption(Connection remoteConn, String content, String evaluatingId, Integer score){

        try {
            PreparedStatement remotePreparedStatement = null;

            String id = ModelUtil.getCleanUUID();
            String insertEvaluating = "insert evaluating_ce_option (id, content , score, evaluatingCEId ,created, deleted) VALUES (?, ?, ?, ?, ?, ?)";
            remotePreparedStatement = remoteConn.prepareStatement(insertEvaluating);
            remotePreparedStatement.setString(1, id);
            remotePreparedStatement.setString(2, content);
            remotePreparedStatement.setInt(3, score);
            remotePreparedStatement.setString(4, evaluatingId);
            remotePreparedStatement.setDate(5, new Date(System.currentTimeMillis()));
            remotePreparedStatement.setBoolean(6, false);
            remotePreparedStatement.execute();
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
